package com.crawler.search.config;

import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;

/**
 * @ClassName: ESConfig
 * @Description: es配置类
 * @Author: zyh
 * @Date: 2020-10-28 10:39
 */
@Configuration
public class ESConfig {

    @Value("${elasticsearch.hosts}")
    private  String hosts ; // 集群地址，多个用,隔开

    @Value("${elasticsearch.port}")
    private  int port ; // 使用的端口号

    private  String schema = "http"; // 使用的协议

    private  ArrayList<HttpHost> hostList = null;

    @Value("${elasticsearch.connectTimeOut}")
    private  int connectTimeOut ; // 连接超时时间

    @Value("${elasticsearch.socketTimeOut}")
    private  int socketTimeOut ; // 连接超时时间

    @Value("${elasticsearch.connectionRequestTimeOut}")
    private  int connectionRequestTimeOut ; // 获取连接的超时时间

    @Value("${elasticsearch.maxConnectNum}")
    private  int maxConnectNum ; // 最大连接数

    @Value("${elasticsearch.maxConnectPerRoute}")
    private  int maxConnectPerRoute ; // 最大路由连接数

    private static final Logger logger = LoggerFactory.getLogger(ESConfig.class);


    @Bean
    public RestHighLevelClient client(){

        hostList = new ArrayList<>();
        String[] hostStrs = hosts.split(",");
        for (String host : hostStrs) {
            hostList.add(new HttpHost(host, port, schema));
        }
        logger.info("初始化 ES 客户端---{}  ,{}", new Object[]{hosts, port});

        RestClientBuilder builder = RestClient.builder(hostList.toArray(new HttpHost[0]));

        // 异步httpclient连接延时配置
        builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
            @Override
            public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
                requestConfigBuilder.setConnectTimeout(connectTimeOut);
                requestConfigBuilder.setSocketTimeout(socketTimeOut);
                requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeOut);
                return requestConfigBuilder;
            }
        });

        // 异步httpclient连接数配置
        builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
            @Override
            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                httpClientBuilder.setMaxConnTotal(maxConnectNum);
                httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);
                return httpClientBuilder;
            }
        });
        RestHighLevelClient client = new RestHighLevelClient(builder);
        return client;
    }
}
